package com.ymatou.iapi.productmanager.testcase;

import com.google.gson.JsonArray;
import com.ymatou.iapi.productmanager.parameter.GetProductPicturesBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.GetProductPicturesCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.List;
import java.util.stream.Collectors;

/**
*查询商品主图列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetProductPictures{
	private static GetProductPicturesBean getproductpicturesBean;
	private static GetProductPicturesCall getproductpicturesCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询商品主图列表");
	}

	@Before
	public void caseUp() {
		getproductpicturesBean=new GetProductPicturesBean();
		getproductpicturesCall=new GetProductPicturesCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getproductpicturesCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductPictures_01() {
		Logger.start(true,"查询商品主图列表");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			//手动调同步mongo
			ProductManagerCallServiceV3.createProduct_sync(productId);
			getproductpicturesBean.setSellerId(sellerId);
			getproductpicturesBean.setProductId(productId);
			getproductpicturesCall.setData(getproductpicturesBean);
			getproductpicturesCall.callService();			
			Logger.verifyEquals("200",getproductpicturesCall.getString("Code"),"验证code");
			JsonArray asJsonArray = getproductpicturesCall.getJsonObject().get("Data").getAsJsonObject().get("Pictures").getAsJsonArray();
			List<String> sOriUrl = new YmtProductsIWapper().selectProductPictures(productId).stream().map(x -> x.get("sOriUrl").toString()).collect(Collectors.toList());
			for (int i =0;i<sOriUrl.size();i++){
				Logger.verifyEquals(asJsonArray.get(i).toString(),"\""+sOriUrl.get(i)+"\"","验证主图");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductPictures_02() {
		Logger.start(true,"查询商品主图列表,productId为空");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			getproductpicturesBean.setSellerId(sellerId);
			getproductpicturesBean.setProductId(null);
			getproductpicturesCall.setData(getproductpicturesBean);
			getproductpicturesCall.callService();
			Logger.verifyEquals("400",getproductpicturesCall.getString("Code"),"验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}